/**
 * 胶头滴管
 */
Dropper = Widget.extend({
	runningAction:null,
	ctor:function(parent){
		this._super();
		parent.addChild(this,30);
		this.setTag(TAG_DROPPER);
		this.init();
		this.initAction();
	},
	init : function(){
		this.setVisible(false);
		this.setCascadeOpacityEnabled(true);
		
		var dropper=new Button(this,10,TAG_DROPPER1,"#dropper4.png",this.callback);
		dropper.setPosition(cc.p(850, 350));
		
	},
	addWater:function(){
		var dropper=this.getChildByTag(TAG_DROPPER1);
		var water=new Button(dropper,10,TAG_WATER,"#water.png",this.callback);
		water.setPosition(cc.p(dropper.width*0.5,dropper.height*0.05));
		var move=cc.moveBy(0.5,cc.p(0, -50));
		water.runAction(cc.sequence(move,cc.callFunc(function() {
			cc.log(water.getPositionY());
			water.removeFromParent(true);
			this.addWater();
		},this)));
		
	},
	initAction:function(){
		var animFrames=[];
		for(var i=4;i>0;i--){
			var str="dropper"+i+".png";
			var frame=cc.spriteFrameCache.getSpriteFrame(str);
			animFrames.push(frame);
		}
		var animation=new cc.Animation(animFrames,0.3)
		this.runningAction=cc.repeat(cc.animate(animation),1);
		this.runningAction.retain();
	},
	callback:function(p){
		var action=gg.flow.flow.action;
		var func=cc.callFunc(function() {
			gg.flow.next();
		},this);
		switch(p.getTag()){
		case TAG_DROPPER1:
			var ber =cc.bezierBy(1, [cc.p(-50,150),cc.p(-150,450),cc.p(-605,180)]);
			var ber1 =cc.bezierBy(1, [cc.p(-50,150),cc.p(-150,450),cc.p(-561,180)]);
			if(action==ACTION_DO1){
				p.runAction(cc.sequence(cc.spawn(ber,cc.sequence(cc.callFunc(function() {
					ll.run.bottle.getChildByTag(TAG_BOTTLE2_ALL).downLine();
				},this)),cc.delayTime(0.8)),cc.spawn(this.runningAction,cc.callFunc(function() {
					ll.run.rack.upLine();
					this.addWater();
				},this)),cc.callFunc(function() {
					if(this.getChildByTag(TAG_DROPPER1).getChildByTag(TAG_WATER)!=null){
						this.getChildByTag(TAG_DROPPER1).getChildByTag(TAG_WATER).removeFromParent(true);
					}
				}, this),ber.reverse(),func));
			}else if(action==ACTION_DO2){
				p.runAction(cc.sequence(cc.spawn(this.runningAction.reverse(),cc.callFunc(function() {
					ll.run.bottle.getChildByTag(TAG_BOTTLE2_ALL).downLine1();
				},this)),cc.spawn(ber1,cc.sequence(cc.callFunc(function() {
					ll.run.bottle.getChildByTag(TAG_BOTTLE2_ALL).downLine();
				},this)),cc.delayTime(0.8)),cc.spawn(this.runningAction,cc.callFunc(function() {
					ll.run.rack.upLine1_1();
					this.addWater();
				},this),cc.callFunc(function() {
					if(this.getChildByTag(TAG_DROPPER1).getChildByTag(TAG_WATER)!=null){
						this.getChildByTag(TAG_DROPPER1).getChildByTag(TAG_WATER).removeFromParent(true);
					}
				}, this),cc.callFunc(function() {
					ll.run.rack.tubeFadein();
				}, this)),ber1.reverse(),func));
			}
			break;
		}
	}
});
